/* -----------------------------------------------------------------------------
 * PROJECT    : lwf
 * FILE       : LogWebErrorTag.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Apr 25, 2011  mycodetalks			       Created
 */
package net.lwf.web.tags;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import net.lwf.exception.ExceptionId;
import net.lwf.web.constant.IWebConstant;

import org.apache.log4j.Logger;

/**
 * Tag logs the error that was caused due to 404, 500 or 503 etc.
 */
public class LogWebErrorTag extends TagSupport {
	
	private static final long serialVersionUID = 1L;
	
	private static final Logger logger = Logger.getLogger(net.lwf.web.tags.LogWebErrorTag.class);
	
	public int doStartTag() throws JspException {
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		Object obj = request.getAttribute(IWebConstant.EXCEPTION_ID);
		String exceptionId = null;
		if(obj != null) {
			exceptionId = (String) obj;
		}
		else {
			exceptionId = ExceptionId.getExceptionID();	
		}
		
		request.setAttribute(IWebConstant.EXCEPTION_ID, exceptionId);
		
		logger.error("Web error occurred... and details are below.");
		logger.error("  Exception Id: " + exceptionId);
		logger.error("           URL: " + request.getAttribute("javax.servlet.error.request_uri"));
		logger.error("   Status code: " + request.getAttribute("javax.servlet.error.status_code"));
		logger.error("       Message: " + request.getAttribute("javax.servlet.error.message"));
		logger.error("  Servlet name: " + request.getAttribute("javax.servlet.error.servlet_name"));
		logger.error("     Exception: " + request.getAttribute("javax.servlet.error.exception"));
		logger.error("Exception type: " + request.getAttribute("javax.servlet.error.exception_type"));

		return SKIP_BODY;
	}

	public int doEndTag() {
		return EVAL_PAGE;
	}

}



